import { Base64 } from "js-base64";
import website from "./website.js";
const CryptoJS = require('crypto-js')
import { encParse } from './rule.js'
// 全局请求封装
export default (path, data = {}, method) => {
	console.log(data)
	// console.log('%c请求拦截：', ' background:orange', data);

	const userInfo = uni.getStorageSync("userInfo");
	console.log(userInfo)
	const token = userInfo ? userInfo.token : "";
	
	// if(userInfo) {
	// 	token = JSON.parse(userInfo).access_token
	// }
	const Authorization =  `Bearer ` + token;
	//    var baseUrl = '/xparkApi/'
	// if(path.indexOf('minio') != -1) {
	// 	baseUrl = '/xparkUploadApi/'
	// }
	// if(path.indexOf('login') != -1) {
	// 	var key = 'zoe_nurse_qazxr8'
	// 	var iv  = 'qwertyuiopasdfgh'
	// 	var str = '123'
	// 	const keyStr = key ? encParse(key) : encParse(defaultKey);
	// 	const ivStr = iv ? encParse(iv) : encParse(defaultIv);
		
	// 	const encryptedStr = CryptoJs.AES.encrypt(str, keyStr, {
	// 	    iv: ivStr,
	// 	    mode: CryptoJs.mode.CBC,
	// 	    padding: CryptoJs.pad.Pkcs7
	// 	});
		
	// 	// 直接toString()是base64格式的字符串
	// 	// ciphertext.toString() 是128位的字符串
	// 	return encryptedStr.toString();
	// }

	// var baseUrl = 'http://in.zynsudu.ml/'
	// var baseUrl = 'http://36.137.244.112:8030/' + baseUrl
	// var baseUrl = 'http://192.168.250.85:9050/'
	// var baseUrl = 'http://192.168.250.43:9050/'
	// var baseUrl = 'http://461c7108.r7.cpolar.top/'
	// var baseUrl = 'http://192.168.250.153:9050/'
	// var baseUrl = 'http://377e795.r6.cpolar.top'
		// var baseUrl = 'http://192.168.10.16:33781/api/frontApph7/'
	var baseUrl = 'https://www.wantongkonggu.cn/api/frontApph7/'
	return new Promise((resolve, reject) => {
		uni.request({
			header: {
				Authorization,
				'Authori-zation': token,
				"Blade-Auth": 'bearer ' +  token || ''
			},
			url: baseUrl + path,
			method: method,
			data: data,
			success(response) {
				console.log(response)
				if(response.statusCode == 200) {
					resolve(response.data);
				} else {
					uni.showToast({
						icon: "none",
						title: '服务响应失败'
					});
					reject(response);
				}
				// console.log('%c响应拦截：', ' background:green', response);

			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.log(err);
				reject(err);
				// uni.hideLoading();
			},
			complete() {
					// uni.hideLoading();
			}
		});
	});
};
